package com.formiaomiao.demain.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.model.UserBean;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.StaticValues;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomeActivity extends BaseActivity {

    @BindView(R.id.rl_root)
    ConstraintLayout rlRoot;

    UserBean userBean = new UserBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        PreferenceUtils.getPreferences(this);
        initData();
        if (!PreferenceUtils.getString(this, PreferenceValues.PASSWORD).equals("") &&
                !PreferenceUtils.getString(this, PreferenceValues.USERNAME).equals("")) {
            LoginInit(PreferenceUtils.getString(this, PreferenceValues.USERNAME),
                    PreferenceUtils.getString(this, PreferenceValues.PASSWORD));
        } else {
            initData();
        }
    }

    private void initData() {
        //渐变动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.6f, 1);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setFillAfter(true);
        rlRoot.startAnimation(alphaAnimation);
        //对动画设置监听，动画播放结束后跳转到新手引导页
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (PreferenceUtils.getBoolean(WelcomeActivity.this, PreferenceValues.FIRSTUSE)) {
                    //不是第一次
                    if (PreferenceUtils.getString(WelcomeActivity.this, PreferenceValues.USERNAME).equals("")) {
                        getLogin(LoginActivity.class);
                    }
                } else {
                    //第一次
                    PreferenceUtils.setBoolean(WelcomeActivity.this, PreferenceValues.FIRSTUSE, true);
                    getLogin(LoginActivity.class);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 登录操作
     *
     * @param user
     * @param password
     */
    public void LoginInit(final String user, final String password) {
        HttpParams params = new HttpParams();
        params.put("userName", user);
        params.put("passWord", password);
        OkGo.<String>post(BaseUrl.USERSIGNIN).params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                userBean = gson.fromJson(response.body(), UserBean.class);
                if (response.body().contains("200")) {
                    StaticValues.nickname = userBean.getData().getNickName();
                    StaticValues.userid = userBean.getData().getId();
                    StaticValues.username = userBean.getData().getUserName();
                    PreferenceUtils.setString(WelcomeActivity.this, PreferenceValues.TOKEN, "Bearer " + userBean.getData().getToken());
                    if (userBean.getData().getUserHead() == null) {
                        StaticValues.userhead = "";
                    } else {
                        StaticValues.userhead = userBean.getData().getUserHead();
                    }
                    RingLogin(user, password);
                } else {
                    System.out.println(userBean.getMsg());
                    getLogin(LoginActivity.class);
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                getLogin(LoginActivity.class);
            }
        });
    }

    /**
     * 登录网易云
     *
     * @param userName
     * @param password
     */
    public void RingLogin(String userName, String password) {
        EMClient.getInstance().login(userName,password,new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Log.d("main", "登录聊天服务器成功！");
                startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                finish();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登录聊天服务器失败！");
            }
        });
    }

    /**
     * 第一次打开软件
     */
    public void getLogin(Class c) {
        startActivity(new Intent(this, c));
        finish();
    }
}
